﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"  Inherits="System.Web.Mvc.ViewPage<DetailedReportViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Detailed Report
</asp:Content>

<asp:Content ID="ContentHeader" ContentPlaceHolderID="ContentHeader" runat="server">
Detailed Report
</asp:Content>

<asp:Content ID="javascript" ContentPlaceHolderID="Javascript" runat="server">
    
<script type="text/javascript">
    $(document).ready(function() {
        $("#filterContainer").hide();
        $("#filterBtn").click(function() {
            $("#filterContainer").toggle("slow");
        });

        $(".productHolder :checkbox").change(function(e) {
            if ($(this).attr('checked') == true) {
                $(this).parents('ul').siblings(".categoryName").children(':checkbox').attr('checked', false);
            }
        });

        $(".categoryName :checkbox").change(function(e) {
            if ($(this).attr('checked') == true) {
                $(this).parent().siblings(".productHolder").find(':checkbox').attr('checked', false);
            }
        });
        
        
    });

    </script>
    
<style media="all" type="text/css">
    ul.productHolder li {float:left; width:220px}
</style>

 <script type="text/javascript">
     $(document).ready(function() {



     var tabApi = $("#accordion").tabs("div.productItems", { tabs: 'h4', effect: 'slide', initialIndex: null }).data("tabs");
         //alert(tabApi.getCurrentTab());
         //$("#accordion").tabs("ul.productHolder ", { tabs: 'h4', effect: 'slide', initialIndex: 1 });
         //var tabApi= $("#accordion").data("tabs");

         $("h4").click(function() {

             if ($(this).hasClass("current")) {
                // alert($(this));
                 $(this).next('div').hide();

             }
         });

     });
</script>
<style>
    /* root element for accordion. decorated with rounded borders and gradient background image */
    #accordion {
    }

    /* accordion header */
    #accordion h4 {
    cursor:pointer;		
    padding:5px;
    border-top:1px solid;
    font-weight:bold;
    font-size:1.3em;
    }

    /* currently active header */
    #accordion h4.current {
    cursor:default;
    background-color:#EFEFEF;
    }

    /* accordion pane */
    #accordion div.productItems{

    display:none;

    }

    div.productItems{
    margin: 5px 0 0;
    padding: 0 0 5px;
    border:0 none;
    }
</style>

</asp:Content>

<asp:Content ID="ContentSubNav" ContentPlaceHolderID="ContentSubNav" runat="server">
<% Html.RenderPartial("Partial/ReportSubMenu"); %>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="clearfix" style="border-bottom:2px solid red;margin:0 0 10px;padding:0 0 15px;">
        <a id="filterBtn">Filter</a>
        <input type="submit" class="clearBtn" value="Clear Filter" onClick="location.href='<%= Url.Action((string)null) %>';return false;">
    
        <div id="<%= Model.ReportData != null ? "filterContainer" : "" %>">
        
            <div class="faux_form">
        <fieldset>
            <% using(Html.BeginForm("DetailedReport", "Enquiry", FormMethod.Get)){ %>
                <%= Html.Hidden("DoSearch", "true") %>
                <div class="clearfix">
                    
                    <label for="" class="">Date From:</label>
                    <%= Html.TextBox("From", Model.From != null ? Model.From.Value.ToShortDateString() : "" , new { @class = "dateInput date" })%>
                </div>
                
                <div class="clearfix">
                    <label for="" class="">Date To:</label>
                    <%= Html.TextBox("To", Model.To != null ? Model.To.Value.ToShortDateString() : "", new { @class = "dateInput date" })%>
                </div>
        
                <div class="clearfix" style="border:none;">
                    
                    <div id="accordion">
                    <% foreach(var catergory in Model.Categories.Keys){
                           var catChecked = Model.CategoryIDs.Contains(catergory.CategoryId);
                            %>
                            
                                
                                
                                <h4><%= catergory.Name %></h4>
                                <div class="productItems">
                                   <div class="clearfix categoryName">
                                    <label style="width:auto" for="CategoryID_<%= catergory.CategoryId %>">Select all <%= catergory.Name %> products</label>
                                    <input class="chk" type="checkbox" value="<%= catergory.CategoryId %>" name="CategoryIDs" id="CategoryID_<%= catergory.CategoryId %>" <%= catChecked ? "checked=\"true\"" : "" %>/> 
                                </div>
                                    
                                    <ul class="productHolder clearfix">
                                        
                                        <% foreach(var product in Model.Categories[catergory]){
                                               var prodChecked = Model.ProductIDs.Contains(product.DrillDownId);
                                               %>
                                            <li class="clearfix">
                                                <label for="ProductID_<%= product.DrillDownId %>"><%= product.Name %></label>
                                                <input class="chk" type="checkbox" value="<%= product.DrillDownId %>" name="ProductIDs" id="ProductID_<%= product.DrillDownId %>" <%= prodChecked ? "checked=\"true\"" : "" %>/> 
                                            </li>
                                        <% } %>
                                    
                                    </ul>
                                </div>                                    
                          
                    <% } %>
                    </div>
                </div>
                <input type="submit" value="Filter">
            <%} %>
        </fieldset>
            </div>
        </div>
        <%= Html.ActionLink("Download", "DetailedReportDownload", this.ViewContext.GetAllExistingRouteValues(), new Dictionary<string, object> { { "Title", "Download" }, { "Class", "btnDownload" } })%>
    </div>
    <%if(Model.ReportData != null){ %>
    <table id="DetailedReport">
        <thead>
            <tr>
                <th>Category</th>
                <th>Product</th>
                <th>Variant</th>
                <th>Source</th>
                <th>Total</th>
                <th>Cost</th>
                <th>Website</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Letter</th>
            </tr>
        </thead>
        <tbody>
        
        <% foreach(var categoryGroup in Model.ReportData.GroupBy(x => new{name = x.Category, val = x.CategoryID})){%>
            
            
            <%--category group start - only show if CategoryID is selected--%>
            <% if (Model.CategoryIDs.Count != 0){  %>
                
                <tr >
                    <td class="ProductCategory " colspan="4"><%= categoryGroup.Key.name %></td>
                    <%--<td class="ProductCategory nrb" >&nbsp;</td><td class="ProductCategory nrb">&nbsp;</td><td class="ProductCategory ">&nbsp;</td>--%>
                    <td class="ProductCategory"><%= categoryGroup.Count() %></td>
                    <td class="ProductCategory">&pound;<%= categoryGroup.Sum(x => x.Amount) %></td>
                    <td class="ProductCategory"><%= categoryGroup.Count(x => x.SourceID == 1) %></td>
                    <td class="ProductCategory"><%= categoryGroup.Count(x => x.SourceID == 2)%></td>
                    <td class="ProductCategory"><%= categoryGroup.Count(x => x.SourceID == 3)%></td>
                    <td class="ProductCategory"><%= categoryGroup.Count(x => x.SourceID == 4)%></td>
                </tr>
                
                
                <% 
                    var categoryGroupEnquires = categoryGroup.Where(x => x.QueryTypeID == (byte)Spotless.Data.Query.QueryTypes.Enquiry);
                    var categoryGroupComplaints = categoryGroup.Where(x => x.QueryTypeID == (byte)Spotless.Data.Query.QueryTypes.Complaint);
                %>
                
                <tr class="bold"><%--Enquiries --%>
                    <td class="ProductCategory ntb">&nbsp;</td>
                    <td class="ProductCategory ntb nlb">&nbsp;</td>
                    <td class="ProductCategory ntb nlb">&nbsp;</td>
                    <td class="ProductCategory ">Enquiries Total</td>
                    <td class="ProductCategory "><%= categoryGroupEnquires.Count()%></td>
                    <td class="ProductCategory ">&pound;<%= categoryGroupEnquires.Sum(x => x.Amount)%></td>
                    <td class="ProductCategory "><%= categoryGroupEnquires.Count(x => x.SourceID == 1)%></td>
                    <td class="ProductCategory "><%= categoryGroupEnquires.Count(x => x.SourceID == 2)%></td>
                    <td class="ProductCategory "><%= categoryGroupEnquires.Count(x => x.SourceID == 3)%></td>
                    <td class="ProductCategory "><%= categoryGroupEnquires.Count(x => x.SourceID == 4)%></td>
                </tr>
                <tr class="bold"><%--Complaints--%>
                    <td class="ProductCategory ntb">&nbsp;</td>
                    <td class="ProductCategory ntb nlb">&nbsp;</td>
                    <td class="ProductCategory ntb nlb">&nbsp;</td>
                    <td class="ProductCategory ">Complaints Total</td>
                    <td class="ProductCategory "><%= categoryGroupComplaints.Count()%></td>
                    <td class="ProductCategory ">&pound;<%= categoryGroupComplaints.Sum(x => x.Amount)%></td>
                    <td class="ProductCategory "><%= categoryGroupComplaints.Count(x => x.SourceID == 1)%></td>
                    <td class="ProductCategory "><%= categoryGroupComplaints.Count(x => x.SourceID == 2)%></td>
                    <td class="ProductCategory "><%= categoryGroupComplaints.Count(x => x.SourceID == 3)%></td>
                    <td class="ProductCategory "><%= categoryGroupComplaints.Count(x => x.SourceID == 4)%></td>
                </tr>
                <% foreach (var categoryGroupSelectionGroup in categoryGroupComplaints.GroupBy(x => new { name = x.Selection, val = x.SelectionID })){ %>
                    <tr>
                        <td class="ProductCategory ntb">&nbsp;</td>
                        <td class="ProductCategory ntb nlb">&nbsp;</td>
                        <td class="ProductCategory ntb nlb">&nbsp;</td>
                        <td class="ProductCategory "><%= categoryGroupSelectionGroup.Key.name ?? "N/A"%></td>
                        <td class="ProductCategory "><%= categoryGroupSelectionGroup.Count()%></td>
                        <td class="ProductCategory ">&pound;<%= categoryGroupSelectionGroup.Sum(x => x.Amount)%></td>
                        <td class="ProductCategory "><%= categoryGroupSelectionGroup.Count(x => x.SourceID == 1)%></td>
                        <td class="ProductCategory "><%= categoryGroupSelectionGroup.Count(x => x.SourceID == 2)%></td>
                        <td class="ProductCategory "><%= categoryGroupSelectionGroup.Count(x => x.SourceID == 3)%></td>
                        <td class="ProductCategory "><%= categoryGroupSelectionGroup.Count(x => x.SourceID == 4)%></td>
                    </tr>
                <%}%>
            <%} else{ %>
                <%--category group end--%>
                <% foreach(var productGroup in categoryGroup.GroupBy(x => new{name = x.Product})){%>
                    
                    <tr >
                        <td class="ProductCategory " colspan="10"><%= categoryGroup.Key.name %></td>
                        <%--<td class="ProductCategory nrb" >&nbsp;</td><td class="ProductCategory nrb">&nbsp;</td><td class="ProductCategory ">&nbsp;</td>--%>
                        <%--<td class="ProductCategory"><%= categoryGroup.Count() %></td>
                        <td class="ProductCategory">&pound;<%= categoryGroup.Sum(x => x.Amount) %></td>
                        <td class="ProductCategory"><%= categoryGroup.Count(x => x.SourceID == 1) %></td>
                        <td class="ProductCategory"><%= categoryGroup.Count(x => x.SourceID == 2) %></td>
                        <td class="ProductCategory"><%= categoryGroup.Count(x => x.SourceID == 3) %></td>--%>
                    </tr>
                    
                    <tr>
                        <td class="ProductCategory ntb">&nbsp;</td>
                        <td class="Product " colspan="3"><%= productGroup.Key.name%></td>
                        <%--<td class="Product nrb">&nbsp;</td><td class="Product ">&nbsp;</td>--%>
                        <td class="Product"><%= productGroup.Count()%></td>
                        <td class="Product">&pound;<%= productGroup.Sum(x => x.Amount)%></td>
                        <td class="Product"><%= productGroup.Count(x => x.SourceID == 1)%></td>
                        <td class="Product"><%= productGroup.Count(x => x.SourceID == 2)%></td>
                        <td class="Product"><%= productGroup.Count(x => x.SourceID == 3)%></td>
                        <td class="Product"><%= productGroup.Count(x => x.SourceID == 4)%></td>
                    </tr>
                    
                    <%--start prdouct group detail--%>
                    <%
                       var productGroupEnquires = productGroup.Where(x => x.QueryTypeID == (byte)Spotless.Data.Query.QueryTypes.Enquiry);
                       var productGroupComplaints = productGroup.Where(x => x.QueryTypeID == (byte)Spotless.Data.Query.QueryTypes.Complaint);
                    %>
                        <tr class="bold"><%--Enquiries --%>
                            <td class="ProductCategory ntb">&nbsp;</td>
                            <td class="Product ntb">&nbsp;</td>
                            <td class="Product ntb nlb">&nbsp;</td>
                            <td class="Product ">Enquiries Total</td>
                            <td class="Product "><%= productGroupEnquires.Count()%></td>
                            <td class="Product ">&pound;<%= productGroupEnquires.Sum(x => x.Amount)%></td>
                            <td class="Product "><%= productGroupEnquires.Count(x => x.SourceID == 1)%></td>
                            <td class="Product "><%= productGroupEnquires.Count(x => x.SourceID == 2)%></td>
                            <td class="Product "><%= productGroupEnquires.Count(x => x.SourceID == 3)%></td>
                            <td class="Product "><%= productGroupEnquires.Count(x => x.SourceID == 4)%></td>
                        </tr>
                        <tr class="bold"><%--Complaints--%>
                            <td class="ProductCategory ntb">&nbsp;</td>
                            <td class="Product ntb">&nbsp;</td>
                            <td class="Product ntb nlb">&nbsp;</td>
                            <td class="Product ">Complaints Total</td>
                            <td class="Product "><%= productGroupComplaints.Count()%></td>
                            <td class="Product ">&pound;<%= productGroupComplaints.Sum(x => x.Amount)%></td>
                            <td class="Product "><%= productGroupComplaints.Count(x => x.SourceID == 1)%></td>
                            <td class="Product "><%= productGroupComplaints.Count(x => x.SourceID == 2)%></td>
                            <td class="Product "><%= productGroupComplaints.Count(x => x.SourceID == 3)%></td>
                            <td class="Product "><%= productGroupComplaints.Count(x => x.SourceID == 4)%></td>
                        </tr>
                        <% foreach (var productGroupSelectionGroup in productGroupComplaints.GroupBy(x => new { name = x.Selection, val = x.SelectionID })){%>
                        <tr>
                            <td class="ProductCategory ntb">&nbsp;</td>
                            <td class="Product ntb">&nbsp;</td>
                            <td class="Product ntb nlb">&nbsp;</td>
                            <td class="Product "><%= productGroupSelectionGroup.Key.name ?? "N/A"%></td>
                            <td class="Product "><%= productGroupSelectionGroup.Count()%></td>
                            <td class="Product ">&pound;<%= productGroupSelectionGroup.Sum(x => x.Amount)%></td>
                            <td class="Product "><%= productGroupSelectionGroup.Count(x => x.SourceID == 1)%></td>
                            <td class="Product "><%= productGroupSelectionGroup.Count(x => x.SourceID == 2)%></td>
                            <td class="Product "><%= productGroupSelectionGroup.Count(x => x.SourceID == 3)%></td>
                            <td class="Product "><%= productGroupSelectionGroup.Count(x => x.SourceID == 4)%></td>
                        </tr>
                        <%}%>        
                    <%--end product group detail--%>
                    
                        <%--Variant group detail Start--%>
                        <%foreach(var varientGroup in productGroup.GroupBy(x => new{name = x.Variation, val = x.VariationID})){%>
                            <tr>
                                <td class="ProductCategory ntb ">&nbsp;</td>
                                <td class="Product ntb">&nbsp;</td>
                                <td class="Variation " colspan="2" ><%= varientGroup.Key.name ?? "N/A"%></td>
                                <%--<td class="Variation">&nbsp;</td>--%>
                                <td class="Variation"><%= varientGroup.Count()%></td>
                                <td class="Variation">&pound;<%= varientGroup.Sum(x => x.Amount)%></td>
                                <td class="Variation"><%= varientGroup.Count(x => x.SourceID == 1)%></td>
                                <td class="Variation"><%= varientGroup.Count(x => x.SourceID == 2)%></td>
                                <td class="Variation"><%= varientGroup.Count(x => x.SourceID == 3)%></td>
                                <td class="Variation"><%= varientGroup.Count(x => x.SourceID == 4)%></td>
                            </tr>
                            <% 
                            var enquires = varientGroup.Where(x => x.QueryTypeID == (byte)Spotless.Data.Query.QueryTypes.Enquiry);
                            var complaints = varientGroup.Where(x => x.QueryTypeID == (byte)Spotless.Data.Query.QueryTypes.Complaint);
                            %>
                            <tr><%--Enquiries --%>
                                <td class="ProductCategory ntb">&nbsp;</td>
                                <td class="Product ntb">&nbsp;</td>
                                <td class="Variation ntb">&nbsp;</td>
                                <td class="VariationItem">Enquiries Total</td>
                                <td class="VariationItem"><%= enquires.Count()%></td>
                                <td class="VariationItem">&pound;<%= enquires.Sum(x => x.Amount)%></td>
                                <td class="VariationItem"><%= enquires.Count(x => x.SourceID == 1)%></td>
                                <td class="VariationItem"><%= enquires.Count(x => x.SourceID == 2)%></td>
                                <td class="VariationItem"><%= enquires.Count(x => x.SourceID == 3)%></td>
                                <td class="VariationItem"><%= enquires.Count(x => x.SourceID == 4)%></td>
                            </tr>
                            <tr> <%--Complaints--%>
                                <td class="ProductCategory ntb">&nbsp;</td>
                                <td class="Product ntb">&nbsp;</td>
                                <td class="Variation ntb">&nbsp;</td>
                                <td class="VariationItem">Complaints Total</td>
                                <td class="VariationItem"><%= complaints.Count()%></td>
                                <td class="VariationItem">&pound;<%= complaints.Sum(x => x.Amount)%></td>
                                <td class="VariationItem"><%= complaints.Count(x => x.SourceID == 1)%></td>
                                <td class="VariationItem"><%= complaints.Count(x => x.SourceID == 2)%></td>
                                <td class="VariationItem"><%= complaints.Count(x => x.SourceID == 3)%></td>
                                <td class="VariationItem"><%= complaints.Count(x => x.SourceID == 4)%></td>
                            </tr>
                            <% foreach (var selectionGroup in complaints.GroupBy(x => new { name = x.Selection, val = x.SelectionID })){%>
                                <tr>
                                    <td class="ProductCategory ntb">&nbsp;</td>
                                    <td class="Product ntb">&nbsp;</td>
                                    <td class="Variation ntb">&nbsp;</td>
                                    <td class="VariationItem"><%= selectionGroup.Key.name ?? "N/A"%></td>
                                    <td class="VariationItem"><%= selectionGroup.Count()%></td>
                                    <td class="VariationItem">&pound;<%= selectionGroup.Sum(x => x.Amount)%></td>
                                    <td class="VariationItem"><%= selectionGroup.Count(x => x.SourceID == 1)%></td>
                                    <td class="VariationItem"><%= selectionGroup.Count(x => x.SourceID == 2)%></td>
                                    <td class="VariationItem"><%= selectionGroup.Count(x => x.SourceID == 3)%></td>
                                    <td class="VariationItem"><%= selectionGroup.Count(x => x.SourceID == 4)%></td>
                                </tr>
                            <%}%>
                        <%}%><%--Variant group detail End--%>
                    <%}%>
                <%}%>
            <%}%>
        </tbody>
    </table>
    <%} %>
</asp:Content>
